package com.scalar.chiptrack.sales;

import java.util.Comparator;
import java.io.Serializable;

/**
 * User: Sujan
 * Date: Mar 14, 2007
 * Time: 1:25:27 PM
 */
public class SplitLineItemsComparator implements Comparator, Serializable
{
    public int compare( Object lineItem1, Object lineItem2 )
    {
        if ( !( lineItem1 instanceof LineItem ) || !( lineItem2 instanceof LineItem ) )
        {
            throw new  ClassCastException ( );
        }

        int order1 = ( ( LineItem )lineItem1).getSplitFromLineItemId();
        int order2 = ( ( LineItem )lineItem2).getSplitFromLineItemId();

        order1 = ( order1 < 0 ) ? 0 : order1;
        order2 = ( order2 < 0 ) ? 0 : order2;

        int result = order1 - order2;

        if ( result == 0 )
        {
            order1 = ( ( LineItem )lineItem1).getLineItemId();
            order2 = ( ( LineItem )lineItem2).getLineItemId();
            order1 = ( order1 < 0 ) ? 0 : order1;
            order2 = ( order2 < 0 ) ? 0 : order2;

            result = order2 - order1; //descending order
        }
        return result;
    }
}
